﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TvViewer.MpTvService;

namespace TvViewer
{
  public partial class EpgForm : Form
  {
    internal class EpgDays
    {
      internal EpgDays(int _index)
      {
        Index = _index;
      }

      internal int Index { get; set; }
      public override string ToString()
      {
        if (Index == 0)
        {
          return "Today";
        }
        else if (Index == 1)
        {
          return "Tomorrow";
        }
        else
        {
          return DateTime.Now.AddDays(Index).DayOfWeek.ToString();
        }
      }
    }
    WebSchedule[] m_schedules;
    ServiceInterface m_server;
    WebChannelGroup m_group;
    public EpgForm(ServiceInterface _server, WebChannelGroup _group)
    {
      InitializeComponent();
      m_server = _server;
      m_group = _group;

      cbDays.Items.Add(new EpgDays(0));
      cbDays.Items.Add(new EpgDays(1));
      cbDays.Items.Add(new EpgDays(2));
      cbDays.Items.Add(new EpgDays(3));
      cbDays.Items.Add(new EpgDays(4));



    }

    private void EpgForm_Load(object sender, EventArgs e)
    {
      try
      {
        WebChannel[] channels = m_server.GetChannelsInTvGroup(m_group.idGroup);
        lvChannels.Items.Clear();
        if (channels.Length > 0)
        {
          for (int i = 0; i < channels.Length; i++)
          {
            String name = channels[i].displayName;
            ListViewItem item = new ListViewItem(name);
            item.Tag = channels[i];
            lvChannels.Items.Add(item);
          }
          lvChannels.Items[0].Selected = true;
          FillEpgList(channels[0], 0);
        }
        lblProgramTitle.Text = string.Empty;
        cbDays.SelectedIndex = 0;
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error loading EPG form, please check your WebServices connection");
      }
    }

    private void lvChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (lvChannels.SelectedItems != null && lvChannels.SelectedItems.Count == 1)
      {
        WebChannel channel = lvChannels.SelectedItems[0].Tag as WebChannel;
        cbDays.SelectedIndex = 0;
        FillEpgList(channel, 0);
      }
    }



    private void cbDays_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (lvChannels.SelectedItems != null && lvChannels.SelectedItems.Count == 1)
      {
        EpgDays day = cbDays.SelectedItem as EpgDays;
        WebChannel channel = lvChannels.SelectedItems[0].Tag as WebChannel;
        FillEpgList(channel, day.Index);
      }
    }

    private void FillEpgList(WebChannel _channel, int _day)
    {
      DateTime begin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + _day);
      DateTime end = begin.AddDays(2);

      txtDay.Text = begin.DayOfWeek.ToString() + ", " + begin.ToShortDateString();

      WebProgram[] epg = null;
      try
      {
        epg = m_server.GetEPGForChannel(_channel.idChannel.ToString(), begin.ToString(), end.ToString());
        m_schedules = m_server.GetAllSchedules();
      }
      catch (Exception ex)
      {
        MessageBox.Show("Couldn't retrieve information from server, check WebService connection");
        m_schedules = null;
      }

      lvEpg.Items.Clear();

      if (m_schedules != null && epg != null)
      {
        foreach (WebProgram p in epg)
        {
          if (p.startTime < begin.AddDays(1))
          {//program starts at selected date
            ListViewItem i = new ListViewItem(p.Title);
            i.Tag = p;
            i.SubItems.Add(p.startTime.ToShortTimeString());
            i.SubItems.Add(p.endTime.ToShortTimeString());

            if (DateTime.Now > p.startTime && DateTime.Now < p.endTime)
            {//currently running
              i.BackColor = Color.Beige;
            }

            if (m_schedules != null)
            {
              foreach (WebSchedule s in m_schedules)
              {
                if (s.idChannel == p.idChannel)
                {
                  if (s.startTime <= p.startTime && s.endTime >= p.endTime)
                  {//program is fully recorded
                    i.BackColor = Color.Red;
                    break;
                  }
                  else if ((s.startTime >= p.startTime && s.startTime <= p.endTime) ||
                           (s.endTime >= p.startTime && s.endTime <= p.endTime))
                  {//program is partially recorded
                    i.BackColor = Color.Orange;
                  }
                }
              }
            }

            lvEpg.Items.Add(i);
          }
        }
      }
    }

    private void lvEpg_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (lvEpg.SelectedItems != null && lvEpg.SelectedItems.Count == 1)
      {
        WebProgram program = lvEpg.SelectedItems[0].Tag as WebProgram;
        lblProgramTitle.Text = program.Title;
        txtEpgDetails.Text = program.description;
      }
    }

    private void recordThisProgramToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (lvEpg.SelectedItems != null && lvEpg.SelectedItems.Count == 1)
      {
        WebProgram program = lvEpg.SelectedItems[0].Tag as WebProgram;
        lblProgramTitle.Text = program.Title;
        txtEpgDetails.Text = program.description;
        //<option value="0">Once</option>
        //<option value="1">Daily</option>
        //<option value="2">Weekly</option>
        //<option value="3">Every time on this channel</option>

        //<option value="4">Every time on every channel</option>
        //<option value="5">Weekends</option>
        //<option value="6">Working days</option>
        RecordingProgramInputBox input = new RecordingProgramInputBox(program);
        if(input.ShowDialog(this) == DialogResult.OK)
        {
          DateTime start = program.startTime.AddMinutes(input.StartEarlier * -1);
          DateTime end = program.endTime.AddMinutes(input.EndLater);
          try
          {
            if (m_server.AddSchedule(program.idChannel, input.RecordingName, start, end, 0))
            {
              lvEpg.SelectedItems[0].BackColor = Color.Red;
            }
            else
            {
              MessageBox.Show("Couldn't add schedule, please control your input");
            }
          }
          catch (Exception ex)
          {
            MessageBox.Show("Error while adding schedule, please check your WebServices connection");
          }
        }
      }
    }
  }
}
